package cn.com.edaily.auth.service;

import cn.com.edaily.auth.dto.UserDTO;
import cn.com.edaily.cmp.User;
import cn.com.edaily.dao.UserMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <pre>类名: AuthService</pre>
 * <pre>描述: 认证服务类</pre>
 * <pre>版权: web_chen@163.com</pre>
 * <pre>日期: 2020/1/30 15:27</pre>
 * <pre>作者: chenwb</pre>
 */

@Service
public class AuthService {

	private static final Integer ONE = 1;

	@Autowired
	private UserMapper userMapper;

	/**
	 * @Description: 用户信息保存
	 * @author chenwb
	 * @date 2020/2/1 21:08
	 * @param userDTO 用户信息dto
	 * @return int 保存结果
	 */
	@Transactional
	public int saveUser(UserDTO userDTO) {
		User user = new User();
		BeanUtils.copyProperties(userDTO, user);
		user.setAge(0); //注册默认年龄为0
		return userMapper.insert(user);

	}

	/**
	 * @param username
	 * @return Boolean
	 * @Description: 注册用户是否重复
	 * @author chenwb
	 * @date 2020/1/31 14:22
	 */
	public Boolean checkUserDuplicate(String username) {
		return findUsersByUsername(username).size() >= ONE;
	}

	/**
	 * @Description: 根据用户名查找用户
	 * @author chenwb
	 * @date 2020/1/31 17:14
	 * @param username
	 * @return findUsersByUsername
	 */
	public List<User> findUsersByUsername(String username) {
		List<User> userList = userMapper.selectByUserName(username);
		if (CollectionUtils.isNotEmpty(userList)) {
			return userList;
		} else {
			return Lists.newArrayList();
		}
	}
}
